function login() {
    ajax({
        url: "/api/user/login",
        type: "post",
        dataType: "json",
        headers: {},
        data: new LoginBean($("input[name=mobile]").val(), md5($("input[name=password]").val()).toUpperCase(),getClientInfo(),osName()),
        success: function (result) {
            switch (result.code) {
                case 0:
                case '0': {
                    if ($("input[type=checkbox]").is(":checked")) {
                        setLocalItem("remember", {
                            mobile: $("input[name=mobile]").val(),
                            password: $("input[name=password]").val()
                        });
                    } else {
                        removeLocalItem("remember");
                    }
                    setLocalItem("user", new AppUserBean(result.data));
                    evalJs("my-index",'reload()');
                    closeWebview();
                    break;
                }
                default: {

                }
            }
            toast(result.msg);
        }
    });
}

function jumpLogin(channel, secret) {
    ajax({
        url: "/api/user/jumpLogin",
        type: "post",
        dataType: "json",
        headers: {},
        data: {
            channel: channel,
            secret: secret,
            regid:getClientInfo(),
            type:osName()
        },
        success: function (result) {
            switch (result.code) {
                case 103:
                case '103': {
                    toast(result.msg);
                    openWebview('account-mobile', {channel: channel, secret: secret});
                    break;
                }
                case 0:
                case '0': {
                    toast(result.msg);
                    setLocalItem("user", new AppUserBean(result.data));
                    evalJs("my-index",'reload()');
                    closeWebview();
                    break;
                }
                default: {
                    toast(result.msg);
                }
            }
        }
    });
}

function forget() {
    openWebview('account-forget');
}

$("input[type=checkbox]").click(function () {
    $(this).parent().toggleClass("selected");
});

function onPageReady() {
    if (getLocalItem("remember")) {
        var remember = getLocalItem("remember");
        $("input[name=mobile]").val(remember.mobile);
        $("input[name=password]").val(remember.password);
        $("input[type=checkbox]").attr("checked", true).parent().toggleClass("selected");
    }
    var channels = [{name: '微信', value: 'wx'}, {name: 'QQ', value: 'qq'}];
    var weixin = null, qq = null;
    plus.oauth.getServices(function (services) {
        for (var i in services) {
            switch (services[i].id) {
                case 'weixin': {
                    weixin = services[i];
                    break;
                }
                case 'qq' : {
                    qq = services[i];
                    break;
                }
            }
        }
    }, function (e) {
        toast("获取分享服务列表失败：" + e.message + " - " + e.code);
    });

    function auth(i) {
        var s = i ? qq : weixin;
        try {
            s.login(function (e) {
                jumpLogin(channels[i].value, s.authResult.openid);
                s.logout();
            }, function (e) {
                s.logout();
                // toast(channels[i].name + "登录认证失败！");
            });
        } catch (e) {
            s.logout();
            // alert(e.toString());
        }
    }

    window.wxLogin = function () {
        auth(0);
    };
    window.qqLogin = function () {
        auth(1);
    };
}